
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>

<!DOCTYPE html>
<html>
    <head>
        <sj:head />
        <script src="js/jquery.cookie.js"></script>
        <script src="js/coms.js"></script>

        <script type="text/javascript">            
           
            $(document).ready(function() {
                
                $('#addKeywordBtn').click(function() {
                    
                    var keyword = $("#keywordBox").val();
                     
                    var keywords = $("#keywords").val() ;
                     
                    var exist = 0;
                    if (keywords!= ""){
                        var keywordList = keywords.split(";");
                       for (var i=0,len=keywordList.length; i<len; i++)
                        { 
                            if (keywordList[i] == keyword) exist++;
                        }
                    }
                     
                    if (exist == 0) {
                    
                        $("#keywordTagArea").append('<button disabled="true">'+keyword +'</button>');
                   
                        if (keywords ==""){
                            keywords = keyword +  ";";
                        } else{
                            keywords = keywords  + keyword +  ";";
                        }
                           
                        $("#keywords").val(keywords) ;
                    } else{
                        alert(keyword +" has previously been added.");
                    }
                });
                
               
                if ($("keywords").val()!=""){
                    var keywordArr =$("#keywords").val().split(";");
                    
                      for (var i=0,len=keywordArr.length; i<len; i++)
                        {
                            if (keywordArr[i]!=""){
                                    $("#keywordTagArea").append('<button disabled="true">'+keywordArr[i] +'</button>');
                            }
                        }
                }
                
               
                $('#addCoauthorBtn').click(function() {
                     
                    var coauthorId= $("#coauthorBox").val();
                    var coauthorName = $("#coauthorBox option:selected").text();
                    var coauthors = $("#coauthors").val();
                     
                    var exist = 0;
                    if (coauthors!= ""){
                        var coauthorList = coauthors.split(";");
                        
                          for (var i=0,len=coauthorList.length; i<len; i++)
                        { 
                            if (coauthorList[i] .split(":")[0] == coauthorId) exist++;
                        }
                    }
                    
                    if (exist == 0) {
                        $("#coauthorTagArea").append('<button disabled="true">'+coauthorName +'</button>');
                    
                        if (coauthors == ""){
                            coauthors = coauthorId+":"+coauthorName + ";";
                        } else {
                            coauthors = coauthors + coauthorId+":"+coauthorName +";";
                        }
                        $("#coauthors").val(coauthors) ;
                    } else{
                        alert(coauthorName +" has previously been added as coauthor.");
                    }
                });
                
                if ($("#coauthors").val()!=""){
                    var coauthorArr =$("#coauthors").val().split(";");
                    for (var i=0,len=coauthorArr.length; i<len; i++)
                        { 
                            if (coauthorArr[i]!=""){
                                $("#coauthorTagArea").append('<button disabled="true">'+coauthorArr[i].split(":")[1] +'</button>');
                            }
                        }
                }

            });
            
        </script>
        <title>Create Publication</title>
    </head>

    <body>
        <s:include value="/jsp/header.jsp">
            <s:param name="user" value="#session.user.username" />
        </s:include>
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span3">
                    <s:action name="selectMenu" executeResult="true"/>
                </div>
                <div class="span9">
                    <div class="hero-unit">

                        <s:form  action="%{action}">
                            <s:hidden name="keywords" id ="keywords" />
                            <s:hidden name="coauthors" id ="coauthors" />
                            <table cellpadding="5" align="center">
                                <tr>
                                    <td><s:text name="fieldLabel.conferenceItem"/></td>
                                    <td> <s:select name="conferenceItem" list="conferenceItemList" listKey="id" listValue="topic" required="true"/> </td>
                                </tr>
                                <tr>
                                    <td><s:text name="fieldLabel.title"/></td>
                                    <td ><s:textfield name="publication.title" size="40" maxlength="40"/>
                                </tr>

                                <tr>
                                    <td><s:text name="fieldLabel.content"/></td>
                                    <td ><s:textarea name="publication.content" cols="30" rows="8"/>
                                </tr>

                                <tr>
                                    <td><s:text name="fieldLabel.firstAuthor"/></td>
                                    <td> <s:select name="firstAuthor" list="userList" listKey="id" listValue="fullname" required="true"/> </td>
                                </tr>

                                <tr>
                                    <td><s:text name="fieldLabel.coAuthors"/></td>
                                    <td>
                                        <div id="coauthorTags">
                                            <s:select list="userList" listKey="id" listValue="fullname" id="coauthorBox" required="true"/>
                                            <input type="button" id="addCoauthorBtn" value="Add"/>
                                            <div id="coauthorTagArea">
                                            </div>
                                        </div>
                                    </td>
                                </tr>   

                                <tr>
                                    <td><s:text name="fieldLabel.presentationType"/></td>
                                    <td> <s:select name="presentationType" list="presentationTypeList" listKey="id" listValue="name" required="true"/> </td>
                                </tr>
                                <tr>
                                    <td><s:text name="fieldLabel.keywords"/></td>
                                    <td>
                                        <div id="keywordTags">
                                            <input type="text" id="keywordBox"/>
                                            <input type="button" id="addKeywordBtn" value="Add"/>
                                            <div id="keywordTagArea">
                                            </div>
                                        </div>
                                    </td>
                                </tr>    

                                <tr>
                                    <td colspan="2">
                                        <s:submit label="button.label.submit"/>
                                    </td>
                                </tr>
                            </table>
                        </s:form>
                    </div>



                    <div class="row-fluid">

                    </div>
                </div>
            </div>
        </div>
        <s:include value="/jsp/footer.jsp"/>
    </body>
</html>
